package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.rabbitmq.RabbitMQConsumerService;
import com.ruoyi.common.utils.rabbitmq.RabbitMQProducerService;
import com.ruoyi.system.domain.UserNotification;
import com.ruoyi.system.domain.dto.SysNotificationDTO;
import com.ruoyi.system.domain.vo.SysNotificationListVo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IUserNotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysNotificationMapper;
import com.ruoyi.system.domain.SysNotification;
import com.ruoyi.system.service.ISysNotificationService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 通知消息Service业务层处理
 * 
 * @author xudengyu
 * @date 2025-03-08
 */
@Service
@Slf4j
public class SysNotificationServiceImpl implements ISysNotificationService 
{
    @Resource
    private SysNotificationMapper sysNotificationMapper;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private RabbitMQProducerService rabbitMQProducerService;

    @Resource
    private IUserNotificationService userNotificationService;



    @Override
    @Transactional
    public boolean pubNotification(SysNotificationDTO sysNotificationDTO) {
        List<Long> notificationSuberIdList = sysNotificationDTO.getNotificationSuberIdList();
        try {
            SysNotification sysNotification = new SysNotification()
                    .setNotificationPuberId(sysNotificationDTO.getNotificationPuberId())
                    .setNotificationContent(sysNotificationDTO.getNotificationContent())
                    .setNotificationType(sysNotificationDTO.getNotificationType())
                    .setNotificationTime(sysNotificationDTO.getNotificationTime());
            insertSysNotification(sysNotification);
            notificationSuberIdList.forEach(i -> {
                String userIdStr = i.toString();
                String queueName = "user." + i + ".queue";

                rabbitMQProducerService.createAndBindQueue(queueName, userIdStr, userIdStr);
                rabbitMQProducerService.sendMessage(userIdStr, JSONUtil.toJsonStr(sysNotificationDTO));
                // 落库
                UserNotification userNotification = new UserNotification()
                        .setNotificationId(sysNotification.getNotificationId())
                        .setUserId(i).setIsRead(UserNotificationServiceImpl.NO_READ);
                userNotificationService.insertUserNotification(userNotification);

            });

            return true;
        } catch (Exception e){
            // 记录错误日志
            log.error("发布通知失败: {}", e.getMessage());
            return false; // 操作失败
        }

    }

    /**
     * 查询通知消息
     * 
     * @param notificationId 通知消息主键
     * @return 通知消息
     */
    @Override
    public SysNotification selectSysNotificationByNotificationId(Long notificationId)
    {
        return sysNotificationMapper.selectSysNotificationByNotificationId(notificationId);
    }

    /**
     * 查询通知消息列表
     * 
     * @param sysNotification 通知消息
     * @return 通知消息
     */
    @Override
    public List<SysNotificationListVo> selectSysNotificationList(SysNotification sysNotification)
    {
        List<SysNotification> sysNotificationList = sysNotificationMapper.selectSysNotificationList(sysNotification);
        ArrayList<SysNotificationListVo> res = new ArrayList<>();
        // 注入puberName
        sysNotificationList.forEach(i-> {
            SysNotificationListVo sysNotificationListVo = BeanUtil.copyProperties(i, SysNotificationListVo.class);
            sysNotificationListVo.setNotificationPuberName(
                    sysUserService.selectUserById(i.getNotificationPuberId()).getUserName());
            res.add(sysNotificationListVo);
                }
        );
        return res;
    }

    /**
     * 新增通知消息
     * 
     * @param sysNotification 通知消息
     * @return 结果
     */
    @Override
    public int insertSysNotification(SysNotification sysNotification)
    {
        return sysNotificationMapper.insertSysNotification(sysNotification);
    }

    /**
     * 修改通知消息
     * 
     * @param sysNotification 通知消息
     * @return 结果
     */
    @Override
    public int updateSysNotification(SysNotification sysNotification)
    {
        return sysNotificationMapper.updateSysNotification(sysNotification);
    }

    /**
     * 批量删除通知消息
     * 
     * @param notificationIds 需要删除的通知消息主键
     * @return 结果
     */
    @Override
    public int deleteSysNotificationByNotificationIds(Long[] notificationIds)
    {
        return sysNotificationMapper.deleteSysNotificationByNotificationIds(notificationIds);
    }

    /**
     * 删除通知消息信息
     * 
     * @param notificationId 通知消息主键
     * @return 结果
     */
    @Override
    public int deleteSysNotificationByNotificationId(Long notificationId)
    {
        return sysNotificationMapper.deleteSysNotificationByNotificationId(notificationId);
    }
}
